package com.duyus.ai.tools;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Component
public class WeatherTool {

    private final WebClient webClient;
    private final String apiKey;

    public WeatherTool(WebClient.Builder webClientBuilder,
                       @Value("${weather.api.key}") String apiKey) {
        this.webClient = webClientBuilder.baseUrl("https://api.openweathermap.org/data/2.5").build();
        this.apiKey = apiKey;
    }

    @Tool(description = "根据城市名查询当前天气情况")
    public Mono<String> getWeather(@ToolParam(description = "查询的条件", required = true) String city) {
        String url = "/weather?q={city}&appid={apiKey}&units=metric";

        return webClient.get()
                .uri(url, city, apiKey)
                .retrieve()
                .bodyToMono(String.class);
    }
}
